#!/bin/bash

#系统更新
system_update(){
  echo '正在更新系统'
  apt update -y
  apt upgrade -y
  echo -e '系统更新完毕.\n'
}

#安装系统软件
install_software(){
  echo '正在安装系统组件'
  BASIC='man gcc make sudo lsof ssh openssl tree vim language-pack-zh-hans'
  EXT='dnsutils iputils-ping net-tools psmisc sysstat'
  NETWORK='curl telnet traceroute wget'
  LIBS='libbz2-dev libpcre3 libpcre3-dev libreadline-dev libsqlite3-dev libssl-dev zlib1g-dev'
  SOFTWARE='git mysql-server zip apache2-utils'
  apt install -y $BASIC $EXT $NETWORK $LIBS $SOFTWARE

  echo '正在清理临时文件'
  apt autoremove
  apt aotuclean

  echo '正在设置中文环境'
  locale-gen zh_CN.UTF-8
  export LC_ALL='zh_CN.utf8'
  echo "export LC_ALL='zh_CN.utf8'" >> /etc/bash.bashrc

  echo '正在启动邮件服务'
  service sendmail start

  echo -e '系统组件安装完毕.\n'
}

#安装NGINX
install_nginx(){
  echo '正在安装 Nginx...'
  if ! which nginx > /dev/null
  then
    wget -P /tmp 'http://nginx.org/download/nginx-1.18.0.tar.gz'
    tar -zxf /tmp/nginx-1.18.0.tar.gz -C /tmp
    cd /tmp/nginx-1.18.0
    ./configure --prefix=/usr/local/nginx
    make && make install
    cd -
    ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
    echo -e 'Nginx 安装完毕.\n'
  else
    echo -e 'Nginx 已存在.\n'
  fi
}

#安装Redis
install_redis(){
  echo '正在安装 Redis...'
  if ! which redis-server > /dev/null
  then
    wget -P /tmp 'http://download.redis.io/releases/redis-5.0.0.tar.gz'
    tar -zxf /tmp/redis-5.0.0.tar.gz -C /tmp
    cd /tmp/redis-5.0.0
    make && make install
    cd -
    echo -e 'Redis 安装完毕.\n'
  else
    echo -e 'Redis 已存在.\n'
  fi
}

#安装pyenv
install_pyenv(){
  echo '正在安装 pyenv...'
  if ! which pyenv > /dev/null
  then
    curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
    export PATH='$HOME/.pyenv/bin:$PATH'
    eval '$(pyenv init -)'
    eval '$(pyenv virtualenv-init -)'
    echo -e 'pyenv 安装完毕.\n'
  else
    echo -e 'pyenv 已存在\n'
  fi
  pyenv update
}

#将pyenv配置写入bashrc $HOME为家目录 $PATH为环境变量
set_pyenv_conf(){
  echo '正在配置 pyenv'
  cat >> $HOME/.bashrc << EOF

  #PyenvConfig
  export PATH="\$HOME/.pyenv/bin:\$PATH"
  eval "\$(pyenv init -)"
  eval "\$(pyenv virtualenv-init -)"
  EOF

  source $HOME/.bashrc
  echo -e 'pyenv 配置完毕.\n'
}